package com.sgb.auction.viewmodule;

import androidx.lifecycle.ViewModel;

import com.sgb.auction.base.BaseApi;
import com.sgb.auction.entity.AuctionListEntity;
import com.sgb.auction.entity.RequestBean;
import com.sgb.auction.fragment.AuctionHallFragment;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.view.areapopup.AddressEntity;
import com.swgk.core.view.selection.CommonPopEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * Time:2021/11/22
 * AUTHOR:F_Q
 * Description:
 */
public class AuctionHallFragmentModel extends ViewModel {
    AuctionHallFragment fragment;
    private boolean isLastPage;

    public void setActivity(AuctionHallFragment auctionHallFragment) {
        this.fragment = auctionHallFragment;
    }

    /**
     * 拍卖大厅列表
     *
     * @param requestBean
     */
    public void AuctionHallList(RequestBean requestBean) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getAuctionHallList(requestBean).enqueue(new ICallBack<BaseEntity<ListResponse<List<AuctionListEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<AuctionListEntity>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)) {
                    isLastPage = data.getData().getCurrentPage() >= data.getData().getTotal();
                    fragment.setAuctionHallList(data.getData().getList(), isLastPage);
                } else {
                    MToast.showToast(fragment.getActivity(), data.getMsg());
                }
            }
        });
    }

    public void getWebView(String no) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getUrlSetting("Auctiondetails").enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)) {
                    String webUrl = data.getData().getUrl() + "?no=" + no;
                    BaseApi.startWeb(fragment.getActivity(), webUrl,123456);
                } else {
                    MToast.showToast(fragment.getActivity(), data.getMsg());
                }
            }
        });
    }

    /**
     * 综合排序处理
     *
     * @param fragment
     * @param type
     */
    public void getSortType(AuctionHallFragment fragment, String type) {
        List<CommonPopEntity> datas = new ArrayList<>();
        switch (type) {
            case "":
            case "1":
                datas.clear();
                datas.add(new CommonPopEntity("1", "综合排序"));
                datas.add(new CommonPopEntity("2", "开始时间"));
                datas.add(new CommonPopEntity("4", "距离"));
                break;
            case "2":
            case "3":
                datas.clear();
                datas.add(new CommonPopEntity("1", "综合排序"));
                datas.add(new CommonPopEntity("3", "结束时间"));
                datas.add(new CommonPopEntity("4", "距离"));
                break;
        }
        fragment.setSortTypeList(datas);
    }

    /**
     * 地址信息
     *
     * @param fragment
     */
    public void getAddressModel(AuctionHallFragment fragment) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getAddressEntityCityList().enqueue(new ICallBack<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data != null && data.getData() != null && data.getData().size() > 0) {
                    fragment.setAddressList(data.getData());
                }
            }
        });

    }
}
